<?php
namespace app\api_wp_v01\controller;

class Shop extends ApiWechatProgram {

	/**
	 * 商店绑定
	 */
	public function bind() {
		
		// 校验传入参数
		$parameter = \think\Request::instance()->post();		// 获取传入数据
		$judge_1 = array_key_exists('account', $parameter) && is_string($parameter['account']) && (1 === preg_match('/^[0-9A-Za-z_]{6,20}$/', $parameter['account']));
		$judge_2 = array_key_exists('password', $parameter) && is_string($parameter['password']) && (1 === preg_match('/^[0-9A-Za-z_]{6,20}$/', $parameter['password']));
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		// 获取当前用户
		$SubscriberModel = \app\common\model\SubscriberModel::get($this->subscriber_id);
		if(null === $SubscriberModel){
			return $this->disposeJson(1, '您的用户信息未找到');
		}else if(0 !== $SubscriberModel->getData('group_id')){
			return $this->disposeJson(1, '您已经绑定了店铺，请先返回解绑');
		};
		
		// 获取商店信息
		$SubscriberGroupModel = \app\common\model\SubscriberGroupModel::where('is_del', 0)->where('account', $parameter['account'])->find();
		if(null === $SubscriberGroupModel){
			return $this->disposeJson(1, '账号/密码不正确，请核实后重试');
		};
		
		// 判断商店密码
		$parameter['password'] = md5($parameter['password'] . \think\Config::get('shop_password_suffix'));
		if($parameter['password'] !== $SubscriberGroupModel->password){
			return $this->disposeJson(1, '账号/密码不正确，请核实后重试');
		};
		
		// 执行操作
		$result = \app\common\model\SubscriberModel::where('id', $this->subscriber_id)->where('group_id', 0)
			->update(['group_id' => $SubscriberGroupModel->getData('id')]);
		if(false == $result){
			return $this->disposeJson(1, '保存用户数据失败，请稍后重试');
		};
		
		return $this->disposeJson(0, '绑定成功');
	}
	
	/**
	 * 商店解除绑定
	 */
	public function unbound() {
		
		// 获取当前用户信息
		$SubscriberModel = \app\common\model\SubscriberModel::get($this->subscriber_id);
		if(null === $SubscriberModel){
			return $this->disposeJson(1, '您的用户信息未找到，请联系管理员');
		}else if(0 === $SubscriberModel->group_id){
			return $this->disposeJson(1, '您当前没有绑定，请返回后重试');
		};
		
		// 执行操作
		$SubscriberModel->group_id = 0;
		$result = $SubscriberModel->save();
		if(false == $result){
			return $this->disposeJson(1, '当前状态不正确，请返回后重试');
		};
		
		// 返回结果
		return $this->disposeJson(0, '操作成功');
	}

	/**
	 * 商店注册
	 */
	public function register() {
		
		// 校验传入参数
		$parameter = \think\Request::instance()->post();		// 获取传入数据
		$judge_1 = array_key_exists('account', $parameter) && is_string($parameter['account']) && (1 === preg_match('/^[0-9a-zA-Z]{6,20}$/', $parameter['account']));
		$judge_2 = array_key_exists('password', $parameter) && is_string($parameter['password']) && (1 === preg_match('/^[0-9a-zA-Z]{6,20}$/', $parameter['password']));
		$judge_3 = array_key_exists('shop_name', $parameter) && is_string($parameter['shop_name']) && (0 < mb_strlen($parameter['shop_name'], 'utf8'));
		$judge_4 = array_key_exists('shop_region', $parameter) && is_string($parameter['shop_region']) && (0 < mb_strlen($parameter['shop_region'], 'utf8'));
		$judge_5 = array_key_exists('shop_address', $parameter) && is_string($parameter['shop_address']) && (0 < mb_strlen($parameter['shop_address'], 'utf8'));
		$judge_6 = array_key_exists('human_name', $parameter) && is_string($parameter['human_name']) && (0 < mb_strlen($parameter['human_name'], 'utf8'));
		$judge_7 = array_key_exists('human_phone', $parameter) && is_string($parameter['human_phone']) && (1 === preg_match('/^[0-9]{11}$/', $parameter['human_phone']));
		if(!$judge_1 || !$judge_2 || !$judge_3 || !$judge_4 || !$judge_5 || !$judge_6){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		// 判断用户名重复
		$judge = \app\common\model\SubscriberGroupModel::where('is_del', 0)->where('account', $parameter['account'])->find();
		if(null !== $judge){
			return $this->disposeJson(1, '该用户名已存在');
		};
		
		// 获取当前用户
		$SubscriberModel = \app\common\model\SubscriberModel::get($this->subscriber_id);
		if(null === $SubscriberModel){
			return $this->disposeJson(1, '您的用户信息未找到，请联系管理员');
		}else if(0 !== $SubscriberModel->getData('group_id')){
			return $this->disposeJson(1, '您已经绑定了店铺，请先解绑');
		};
		
		// 密码加盐
		$parameter['password'] = md5($parameter['password'] . \think\Config::get('shop_password_suffix'));
		
		// 执行操作
		$time = time();
		\app\common\model\Model::startTrans();
		try{
			
			// 保存店铺数据
			$time = time();
			$SubscriberGroupModel = new \app\common\model\SubscriberGroupModel();
			$SubscriberGroupModel->account = $parameter['account'];
			$SubscriberGroupModel->password = $parameter['password'];
			$SubscriberGroupModel->title = $parameter['shop_name'];
			$SubscriberGroupModel->region = $parameter['shop_region'];
			$SubscriberGroupModel->address = $parameter['shop_address'];
			$SubscriberGroupModel->contacts_name = $parameter['human_name'];
			$SubscriberGroupModel->contacts_phone = $parameter['human_phone'];
			$SubscriberGroupModel->create_time = $time;
			$result = $SubscriberGroupModel->save();
			if(false == $result){
				throw new \Exception('保存用户数据失败');
			};
			
			// 绑定申请人
			$result = \app\common\model\SubscriberModel::where('id', $this->subscriber_id)->where('group_id', 0)
				->update(['group_id' => $SubscriberGroupModel->id]);
			if(false == $result){
				throw new \Exception('绑定申请人失败');
			};
			
			\app\common\model\Model::commit();
		
		}catch(\Exception $error){
			
			\app\common\model\Model::rollback();
			$message = is_string($error->getMessage()) ? $error->getMessage() : json_encode($error->getMessage());
			return $this->disposeJson(1, $message);
		};
		
		// 返回结果
		return $this->disposeJson(0, '注册成功');
	}
	
	/**
	 * 获取信息
	 */
	public function get_info() {
		
		// 获取当前用户信息
		$SubscriberModel = \app\common\model\SubscriberModel::get($this->subscriber_id);
		if(null === $SubscriberModel){
			return $this->disposeJson(1, '您的用户信息未找到，请联系管理员');
		};
		
		// 获取店铺信息
		$SubscriberGroupModel = \app\common\model\SubscriberGroupModel::get($SubscriberModel->group_id);
		if((null === $SubscriberGroupModel) || (1 === $SubscriberGroupModel->getData('is_del'))){
			return $this->disposeJson(1, '您的店铺信息未找到，请联系管理员');
		};
		
		// 统计店铺回收数量
		$number_count = \app\common\model\RecycleOrderNumberModel::where('group_id', $SubscriberGroupModel->id)->sum('number');
		
		// 获取店铺成员
		$member_count = \app\common\model\SubscriberModel::where('group_id', $SubscriberGroupModel->id)->count('id');
		
		// 生成二维码
		$QrCode = new \CodeItNow\BarcodeBundle\Utils\QrCode('123');
		$QrCode->setPadding(0);
		$QrCode->setSize(130);
		$qrcode_base64 = 'data:'.$QrCode->getContentType().';base64,'.$QrCode->generate();
		
		// 返回结果
		return $this->disposeJson(0, '', array(
			'qrcode' => $qrcode_base64,
			'info' => array(
				'account' => $SubscriberGroupModel->account,
				'name' => $SubscriberGroupModel->title,
				'region' => $SubscriberGroupModel->region,
				'address' => $SubscriberGroupModel->address,
				'human_name' => $SubscriberGroupModel->contacts_name,
				'human_phone' => $SubscriberGroupModel->contacts_phone,
				'is_disable' => (1 === $SubscriberGroupModel->is_disable) ? true : false,
			),
			'member_count' => $member_count,
			'number_count' => round($number_count, 4),
		));
	}
}